package com.lcz.googleplay.activity;

import android.os.Bundle;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.FrameLayout;

import com.lcz.googleplay.R;
import com.lcz.googleplay.adapter.FragmentAdapter;
import com.lcz.googleplay.fragment.FragmentFactory;
import com.lcz.googleplay.utils.ToastUtils;
import com.lcz.googleplay.utils.UIUtils;
import com.lcz.googleplay.widget.ToolBarActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends ToolBarActivity implements SearchView.OnQueryTextListener {

    @BindView(R.id.main_pts)
    PagerTabStrip pager_tab_strip;
    @BindView(R.id.main_vp)
    ViewPager mViewPage;
    @BindView(R.id.main_fl_menu)
    FrameLayout mFl_menu;
    @BindView(R.id.main_dl)
    DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mDrawerToggle;
    //条目标题
    String[] tab_name = UIUtils.getTab_name();

    @Override
    protected void initView() {
        super.initView();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        setTitle("应用程序");
//        getSupportActionBar().setLogo(R.mipmap.ic_launcher);
        // 打开toolbar返回开关
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        // 设置标签下划线的颜色
        pager_tab_strip.setTabIndicatorColor(getResources().getColor(R.color.indicatorcolor));
        // DrawerLayout、ActionBarDrawerToggle 关联toolbar
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close);
        mDrawerToggle.syncState();
        mDrawerLayout.setDrawerListener(mDrawerToggle);
        //添加条目
        mViewPage.setAdapter(new FragmentAdapter(getSupportFragmentManager(), tab_name));
        mViewPage.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                //切换界面时请求服务器
                FragmentFactory.createFragment(position).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
        //搜索控件监听
        searchView.setOnQueryTextListener(this);
        return super.onCreateOptionsMenu(menu);
    }

    /*toolbar菜单条目点击监听*/
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_search:
                ToastUtils.show("搜搜哦！！");
                break;
        }
        return super.onOptionsItemSelected(item);
    }


    /*搜索提交*/
    @Override
    public boolean onQueryTextSubmit(String s) {
        ToastUtils.show(s);
        return true;
    }

    /*搜索文本改变*/
    @Override
    public boolean onQueryTextChange(String s) {
//        ToastUtils.show(s);
        return true;
    }


}
